package org.agridata.akvisualization.controller.xuqin;

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.agridata.akvisualization.doMain.entity.Livestock;
import org.agridata.akvisualization.doMain.dto.LivestockDto;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.LivestockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/livestockPrice")
@Api(tags = "畜禽价格")
public class LivestockPriceController {

    @Autowired
    private LivestockService livestockService;

    @ApiOperation("畜禽价格导入")
    @PostMapping("/livestockImport")
    public Result livestockImport(@RequestPart("file") MultipartFile file, String dates,String cycle){
        try {
            List<Livestock> memberList = EasyExcel.read(file.getInputStream())
                    .head(Livestock.class)
                    .sheet()
                    .doReadSync();
            return livestockService.importLivestock(memberList,dates,cycle);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }

    }

    @ApiOperation("畜禽价格列表")
    @PostMapping("/livestockList")
    public Result livestockList(@RequestBody LivestockDto livestockDto){
        return livestockService.livestockListPage(livestockDto);
    }

    @ApiOperation("首页价格走势展示")
    @PostMapping("/perMensem")
    public Result pricePerMensem(){
        return livestockService.pricePerMensems();
    }



}
